package com.tss.controller;


import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tss.pojo.Apply;
import com.tss.pojo.Course;
import com.tss.pojo.Result;
import com.tss.service.ApplyService;
import com.tss.service.CertificateService;
import com.tss.service.CourseService;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("course")
public class CourseController {
	
	@Autowired
	private CourseService courseService;
	
	@Autowired
	private CertificateService certificateService;
	
	@Autowired
	private ApplyService applyService;
	
	
	/**
	 * 获取课程分页列表
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("list")
	@ResponseBody
	public String list(@RequestParam(required=true,defaultValue="1") Integer pageNum,
            @RequestParam(required=false,defaultValue="10") Integer pageSize){ 
 
		PageHelper.startPage(pageNum, pageSize);
		List<Course> list = courseService.findAllCourse();
		PageInfo<Course> p = new PageInfo<>(list);
		return JSONObject.fromObject(p).toString();
	}
	
	/**
	 * 添加课程
	 * @param course
	 * @return
	 */
	@RequestMapping("add")
	@ResponseBody
	public String add(Course course,String start,String end){
		if(start!=null&&end!=null){
			course.setStartTime(new Date(Long.valueOf(start)));
			course.setEndTime(new Date(Long.valueOf(end)));
		}
		Result result = courseService.createCourse(course);
		return result.toString();
	}
	
	/**
	 * 修改课程
	 * @param course
	 * @return
	 */
	@RequestMapping("update")
	@ResponseBody
	public String update(Course course,String start,String end){
		if(start!=null&&end!=null){
			course.setStartTime(new Date(Long.valueOf(start)));
			course.setEndTime(new Date(Long.valueOf(end)));
		}
		return courseService.updateCourse(course).toString();
	}
	
	
	/**
	 * 删除课程
	 * @param id
	 * @return
	 */
	@RequestMapping("delete")
	@ResponseBody
	public String delete(Long id){
		return courseService.deleteCourse(id).toString();
	}
	
	
	/**
	 * 查看课程
	 * @param id
	 * @return
	 */
	@RequestMapping("select")
	@ResponseBody
	public String select(String id){
		Result result = new Result();
		Course course = courseService.findCourse(Long.valueOf(id));
		if(course==null){
			result.setErrorMessage("课程不存在！");
			return result.toString();
		}
		result.setSuccessMessage("获取课程成功！");
		result.addResult("course", course);
		return result.toString();
	}
	
	/**
	 * 课程结束
	 * @param id
	 * @return
	 */
	@RequestMapping("over")
	@ResponseBody
	public String over(Long id){
		Result result = new Result();
		result = courseService.overCourse(id);
		if(result.getStatus()==Result.STATUS_CODE_SERVER_ERRORS){
			return result.toString();
		}
		List<Apply> list = applyService.listApply(id);
		if(list==null||list.isEmpty()){
			result.setErrorMessage("没有任何学员毕业！");
			return result.toString();
		}
		for(Apply apply:list){
			System.out.println(certificateService.initCertificate(apply).toString());
			
		}
		result.setSuccessMessage("结课成功！");
		return result.toString();
	}
	
	
	
	
	
}
